//
//  CustomModalTransition.swift
//  VCTranstion
//
//  Created by 李煜 on 2024/9/13.
//

import UIKit

class CustomModalTransition: NSObject, UIViewControllerAnimatedTransitioning {
  func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 0.5 // 设置转场的持续时间
  }
  
  func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let fromViewController = transitionContext.viewController(forKey: .from),
          let toViewController = transitionContext.viewController(forKey: .to) else { return }
    
    let containerView = transitionContext.containerView
    containerView.addSubview(toViewController.view)
    
    // 设置初始状态
    toViewController.view.alpha = 0.0
    
    UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
      // 进行动画
      toViewController.view.alpha = 1.0
    }) { _ in
      // 完成转场
      transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    }
  }
}
